//
//  CategoryViewController.m
//  We365
//
//  Created by Bui Thien An on 12/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CategoryViewController.h"
#import "ListViewController.h"
#import "SearchViewController.h"
#import "BookmarkViewController.h"

@implementation CategoryViewController
@synthesize category;
@synthesize table;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    if(category == nil)
        category = [[Category alloc] init];
    [category getAllCategories];
    [table reloadData];
    
    table.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:BGTable]];
    table.separatorColor = [UIColor yellowColor];
}

- (void)viewDidUnload
{
    [self setTable:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillDisappear:(BOOL)animated
{
    // call Back function.
    if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) {
        NSLog(@"goBack function");
        [UIView beginAnimations:@"animation" context:nil];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:NO];
        [self.navigationController popViewControllerAnimated:NO];
        [UIView commitAnimations];
    }

    [super viewWillDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSString *)title
{
    return @"All Categories";
}

- (void)dealloc
{
    [category release];
    [table release];
    [super dealloc];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [category.list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:identifier];
    
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];      
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        /*
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(0, 0, 32, 32)];
        [button setBackgroundImage:[UIImage imageNamed:@"Search.png"] forState:UIControlStateNormal];    
        [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor = [UIColor clearColor];
        cell.accessoryView = button;*/         
    }
    
    Category *temp = [category.list objectAtIndex:[indexPath row]];    
    cell.textLabel.text = temp.CategoryName;
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", temp.CategoryName]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Category *temp = [category.list objectAtIndex:[indexPath row]];
    
    if([temp.CategoryName isEqualToString:@"Favorite"])
    {
        BookmarkViewController *bookmarkViewControoler = [[BookmarkViewController alloc] init];
        [self.navigationController pushViewController:bookmarkViewControoler animated:YES];
        [bookmarkViewControoler release];
    }
    else
    {
        ListViewController *listViewController = [[ListViewController alloc] initWithCategory:temp andKey:@""];
        [self.navigationController pushViewController:listViewController animated:YES];
        [listViewController release];
    }   
}



@end
